//
//  PainterIndexVolume.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/16.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterIndexVolume.h"

using namespace std;
using namespace ytx;

@implementation PainterIndexVolume

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    IndexData * data = (IndexData *)indexPosition->getData();

    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    float tempX = rect.getBeginX();
    float tempY = 0;
    float KX = tempX;
    float zoomX;
    float endY = rect.getEndY();
    float lineWidth = style.quoteLineWidth;
    float lineWidthHalf = lineWidth * 0.5f;
    UpDropStatus::UpDropStatus upDropStatus;

    vector<double> & volumeData = data->indexData[0];

    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();

    int i = startIndex;
    int len = endIndex;

    zoomX = rect.getZoomX();
    float width = zoomX/6 >= lineWidthHalf ? lineWidthHalf : zoomX/6;

    double vol = 0;
    string color;
    for (; i < len; i++) {

        upDropStatus = data->getUpDropStatus(i);
        switch (upDropStatus) {
            case UpDropStatus::Up:
                color = style.defaultRed;
                break;
            case UpDropStatus::Drop:
                color = style.defaultGreen;
                break;
            case UpDropStatus::Balance:
                color = style.avgOfVolumeColor;
                break;
        }

        vol = volumeData[i];
        if (vol != 0 && !isnan(vol)) {
            tempY = rect.getPxY(vol);
            [ctx fillRectLeftBottomWithX:KX+width withY:endY withWidth:zoomX - 2*width withHeight:endY-tempY withFillColor:[NSString stringWithUTF8String:color.c_str()]];
//            ctx.setFillStyle(color).fillRect(KX + lineWidthHalf, beginY, width, tempY - beginY, color);
        }
        KX += zoomX;
    }
}


@end
